package com.fmdrama;

import java.util.List;

import com.fmdrama.bean.Profile;
import com.fmdrama.bean.Video;
import com.fmdrama.db.ProfileDao;
import com.fmdrama.db.VideoDao;
import com.fmdrama.fetcher.HomeFetcher;
import com.fmdrama.fetcher.PlayFetcher;
import com.fmdrama.util.Configuration;
import com.fmdrama.util.LOG;

/**
 * 爬行者，主要负责调度抓取器和持久化数据
 * @author Administrator
 */
public class Crawler {

	private HomeFetcher homeFetcher = new HomeFetcher();
	private PlayFetcher playFetcher = new PlayFetcher();
	private ProfileDao profileDao = new ProfileDao();
	private VideoDao videoDao = new VideoDao();
	
	/**
	 * 开始抓取
	 */
	public void start(){
		Main.self.status.reset();
		new Thread(){
			public void run() {
				while(Main.self.started){
					try{
						crawl();
						if(Main.self.started){
							Main.self.status.loopCountIncr();
							long sleep = Configuration.getLong("fetch.sleep", 0);
							if(sleep > 0){
								LOG.info("该轮抓取已完成，休息" + sleep + "(毫秒)，进行下一轮抓取");
								Thread.sleep(sleep);
							}
						}
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
		}.start();
	}
	
	/**
	 * 爬行，先抓取首页数据，然后进入每个播放页面抓取数据
	 */
	private void crawl(){
		List<Profile> profiles = homeFetcher.fetch(); //抓取首页
		if(null != profiles){
//			profiles.clear();
//			Profile f = new Profile();
//			f.setId(2272);
//			f.setCategory("2");
//			profiles.add(f);
			Main.self.status.setTotal(profiles.size());
			for(int i=0; i<profiles.size()&&Main.self.started; i++){ //循环首页抓取到的数据，进入每个播放页面抓取数据
				Profile profile = profiles.get(i);
				try{
					profileDao.insertOrUpdate(profile); //持久化首页数据
				}catch (Exception e) {
					e.printStackTrace();
				}
				List<Video> videos = playFetcher.fetch(profile); //抓取播放页面数据
				try{
					if(null != videos && videos.size() > 0){
						videoDao.insert(videos); //持久化播放页面数据
						LOG.info(profile.toString() + "， 抓取：" + videos.size());
					}
				}catch (Exception e) {
					e.printStackTrace();
				}
				Main.self.status.setComplete(i+1); //更新进度
				//exportFile(profiles.get(i).getId(), videos);
			}
		}
	}
	
	/**
	 * 停止抓取
	 */
	public void stop(){
		homeFetcher.close();
		playFetcher.close();
	}
	
//	private void exportFile(String profileId,List<Video> videos){
//		try {
//			String path = "d:/fmdrama/" + profileId + "/";
//			for(Video video : videos){
//				String html = "";
//				if(null != video.getTudou()){
//					html += "<object type=\"application/x-shockwave-flash\" data=\"http://js.tudouui.com/bin/lingtong/PortalPlayer_75.swf\" width=\"400px\" height=\"300px\" id=\"insertplayer\" style=\"visibility: visible;\"><param name=\"quality\" value=\"high\"><param name=\"wmode\" value=\"opaque\"><param name=\"allowfullscreen\" value=\"true\"><param name=\"allowscriptaccess\" value=\"always\"><param name=\"src\" value=\"http://js.tudouui.com/bin/lingtong/PortalPlayer_75.swf\"><param name=\"name\" value=\"insertplayer\"><param name=\"flashvars\" value=\"listType=1&amp;iid={url}&amp;listCode=hQtzrByo_m0&amp;icode=u9KaJdz0sSA&amp;hd=2&amp;ol=1&amp;phd=2&amp;shareBtn=0&amp;topBarFull=0&amp;topBarNor=0&amp;likeBtn=0&amp;sogouBtn=0&amp;menu=false&amp;autoPlay=true\"></object>";
//					html = html.replace("{url}", video.getTudou());
//				}
//				if(null != video.getYukou()){
//					html += "\n<object type=\"application/x-shockwave-flash\" data=\"http://www.koreayh.com/userfiles/media/tools/ykplayer-2.swf\" id=\"movie_player\" style=\"visibility: visible;\" width=\"400px\" height=\"300px\"><param name=\"menu\" value=\"false\"><param name=\"wmode\" value=\"opaque\"><param name=\"allowfullscreen\" value=\"true\"><param name=\"name\" value=\"movie_player\"><param name=\"flashvars\" value=\"VideoIDS={url}&winType=BDskin&isShowRelatedVideo=false&partnerid=XMTI=&show_ce=0&showsearch=0&p=1&menu=false&isAutoPlay=true\"></object>";
//					html = html.replace("{url}", video.getYukou());
//				}
//				
//				File file = new File(path + video.getId() + ".html");
//				if(!file.getParentFile().exists()){
//					file.getParentFile().mkdirs();
//				}
//				if(!"".equals(html)){
//					FileOutputStream fos = new FileOutputStream(file);
//					fos.write(html.getBytes());
//					LOG.info("请查看文件 " + file);
//				}
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
}
